import { JdUnit } from "./jd-unit-service";
/**
 * 获取单位树
 */

import { useMutation, useQuery } from "react-query";
import { useRequest } from "../utils/request";
import { IPage, IPageParams } from "../types";

// /**
//  * 获取单位树接口
//  */
// export const useGetUnitById = (queryParams: { unitId: string }) => {
//   const request = useRequest<SysUnit>();
//   return useQuery(
//     ["getUnitByIdVO", queryParams],
//     () => request("/admin/unit/getUnitByIdVO", { data: queryParams }),
//     {
//       enabled: queryParams.unitId ? true : false,
//     }
//   );
// };

/**
 * !获取社区接口
 */
export const useGetSqUnit = (queryParams: {
  unitId: string;
  sqUnitName?: string;
}) => {
  const request = useRequest<ISqUnit[]>();
  return useQuery(["getSqUnitBySearch", queryParams], () =>
    request("/sqUnit/getSqUnitBySearch", { data: queryParams })
  );
};

/**
 * !添加社区接口
 */
export const useAddSqUnit = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { unitId: string; sqUnitName: string }) => {
    const result = request("/sqUnit/addSqUnit", {
      data: params,
      method: "PUT",
    });
    return result;
  });
};

/**
 * !修改社区接口
 */
export const useUpdateSqUnit = () => {
  const request = useRequest<string>();
  return useMutation(
    async (params: { sqUnitId: string; sqUnitName: string }) => {
      const result = request("/sqUnit/updateSqUnit", {
        data: params,
        method: "POST",
      });
      return result;
    }
  );
};

/**
 * !删除社区接口
 */
export const useDeleteSqUnit = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { sqUnitId: string }) => {
    const result = request("/sqUnit/deleteSqUnitById", {
      data: params,
      method: "DELETE",
    });
    return result;
  });
};

/**
 * !获取单位类型
 */
export const useGetUnitType = () => {
  const request = useRequest<IUnitType[]>();
  return useQuery(["getUnitTypeList"], () =>
    request("/unit/getUnitTypeList", { data: {} })
  );
};

/**
 * !根据单位类型获取到单位列表
 * @param queryParams
 * @returns
 */
export const useGetUnitBypcs = (queryParams: { unitType: number }) => {
  const request = useRequest<SysUnit[]>();
  return useQuery(["getUnitBypcs", queryParams], () =>
    request("/unit/getUnitBypcs", { data: queryParams })
  );
};

/**
 * !获取单位树接口
 */
export const useGetUnitTree = (queryParams: { unitId: string }) => {
  const request = useRequest<SysUnitTree>();
  return useQuery(["getUnitTreeById", queryParams], () =>
    request("/unit/getUnitTreeById", { data: queryParams })
  );
};

/**
 * !获取子单位列表
 */
export const useUnitList = (queryParams: QueryUnitListParams) => {
  const request = useRequest<IPage<SysUnit>>();
  return useQuery(["getUnitChildrenByIdPage", queryParams], () =>
    request("/unit/getUnitChildrenByIdPage", { data: queryParams })
  );
};

/**
 * !删除单位
 */

export const useDeleteUnitTree = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { unitId: string }) => {
    const result = request("/unit/removeUnitById", {
      data: params,
      method: "DELETE",
    });
    return result;
  });
};

/**
 * !添加单位
 */
export const useAddUnit = () => {
  const request = useRequest<string>();
  return useMutation(async (params: AddUnitType) => {
    const result = request("/unit/addUnit", {
      data: params,
      method: "POST",
    });
    return result;
  });
};

/**
 * !更新单位
 */

export const useUpdateUnit = () => {
  const request = useRequest<string>();
  return useMutation(async (params: UpdateUnitType) => {
    const result = request("/unit/updateUnitById", {
      data: params,
      method: "POST",
    });
    return result;
  });
};

export type ISqUnit = {
  //社区主键
  sqUnitId: string;
  //社区名称
  sqUnitName: string;
  //所属派出所ID
  unitId: string;
};

/**
 * 单位类型
 */
export type IUnitType = {
  unitTypeId: number;
  unitTypeName: string;
};

/**
 * 搜索单位的参数
 */
export type QueryUnitListParams = {
  unitId: string;
  unitName?: string;
} & IPageParams;

/**
 * 添加单位类型
 */
export type AddUnitType = {
  unitName: string;
  unitCode: string;
  orderId: number;
  unitShortName: string;
};

/**
 * 修改单位类型
 */
export type UpdateUnitType = {
  unitId: string;
} & AddUnitType;
/**
 * 单位树结构
 */
export type SysUnitTree = {
  children: SysUnit[];
} & SysUnit;

/**
 * 单位结构
 */
export type SysUnit = {
  /**
   * 单位 ID
   */
  unitId: string;

  /**
   * 单位名称
   */
  unitName: string;

  /**
   * 单位简称
   */
  unitShortName: string;
  /**
   * 单位层级
   * ignore insert
   */
  unitLevel: number;

  /**
   * 单位编码
   */
  unitCode: string;

  /**
   * 是否父节点 0=否, 1=是
   */
  isParent: number;

  /**
   * 创建时间
   */
  createTime: string;

  /**
   * 更新时间
   */
  updateTime: string;

  /**
   * 机构类别
   */
  unitType: string;

  /**
   * 父单位编码
   */

  parentId: string;

  /**
   * 父单位名称
   */
  parentName: string;

  /**
   * 排序
   */
  orderId: number;

  /**
   * 街道单位ID
   */
  jdUnitId: string;
};
